#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtCore>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);
    this->readFile();
    this->Mode=T_Mode;
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::readFile()
{
    QDir q;
    //qDebug()<<"\n current Dir" <<q.currentPath() << "\n Home path" << q.homePath() << "\n root Path" << q.rootPath() << "\n";
    QString check = q.currentPath();
    QStringList checkList = check.split(QDir::separator());
#ifdef Q_OS_DARWIN
    checkList.removeLast();
    checkList.removeLast();
    checkList.removeLast();
    qDebug()<<checkList;
#endif
    check = checkList.join(QDir::separator());
    qDebug()<<check;
    check.append(QDir::separator() );
    check.append("boshiamy_t.db");
    //qDebug() << QFile::exists(check)<<"Filename"<<check;
    if (!QFile::exists(check)){
        QMessageBox::warning(this,"無輸入法參考檔","請至官網下載 ibus輸入法表格，將輸入法表格檔之boshiamy_t.db檔案放置於本程式同一目錄下。 ");

    }else{
    QFile file(":/liu-uni.trial.txt");
    qDebug() << file.open(QIODevice::ReadOnly);
    {
        QTextStream in(&file);
        in.readLine((qint64)80);//略過第一行
        while(!in.atEnd()){
            QString a = in.readLine((qint64)64);
            QString key = a.section("",0,5).simplified(), //第一段是編碼
                    word = a.section('~',1,1), //第二段是漢字本身
                    unicode = a.section('~',2,2); //第三段是Unicode編碼
            bool standard =  (!a.section('~',3,3).compare("v")) ; // 是否是建議碼
            bool simplify =  (!a.section('~',4,4).compare("v")) ; // 是否是簡碼
            if(Unicode.contains(unicode))
            {
                QStringList tmp;
                if(standard){
                    tmp = Standard_key.value(word);
                    tmp << key;
                    Standard_key.insert(word,tmp);
                }
                if(simplify){
                    tmp = Simplify_key.value(word);
                    tmp << key;
                    Simplify_key.insert(word,tmp);
                }
            }
            else
            {
                Unicode.insert(unicode,word);
                QStringList tmp;
                tmp.append(key);
                if(standard)
                    Standard_key.insert(word,tmp);
                if(simplify)
                    Simplify_key.insert(word,tmp);
            }
        }
    }
    file.close();
    }
}


void MainWindow::on_action_3_triggered(bool checked)
{
    if (checked == true)
    {
        Mode = T_Mode;
        check_SP = false;
        check_T = true;
        this->ui->action_2->setChecked(false);
    }
    else
    {
        Mode = SP_Mode;
        check_SP = true;
        check_T = false;
        this->ui->action_2->setChecked(true);
    }
}

void MainWindow::on_action_2_triggered(bool checked)
{
    if (checked == true)
    {
        Mode = SP_Mode;
        check_T = false;
        check_SP = true;
        this->ui->action_3->setChecked(false);
    }
    else
    {
        Mode = T_Mode;
        check_T = true;
        check_SP = false;
        this->ui->action_3->setChecked(true);
    }
}
QStringList MainWindow::Separate(QString str)
{
    QStringList temp;
    for(int i=0 ; i< str.length();i++){
        temp.append(str.at(i));
        //qDebug()<< str.at(i);
    }
    //qDebug()<<temp;
    return temp;
}
QString MainWindow::GetCode(QString str)
{
    QString temp = "";
    if( this->Mode == SP_Mode)
    {
        if (this->Simplify_key.contains(str))
        {
            temp = this->Simplify_key.value(str).first();
            qDebug()<< temp;
        }
        else if (this->Standard_key.contains(str))
        {
            temp = this->Standard_key.value(str).first();
            qDebug()<< temp;
        }
    }
    else if(this->Mode == T_Mode)
    {
        if (this->Standard_key.contains(str))
        {
            temp = this->Standard_key.value(str).first();
            qDebug()<< temp;
        }
    }
    if (!temp.compare(""))
    {
        temp = str;
        qDebug() << temp;
    }
   if ( this->Standard_key.isEmpty())
       temp.clear();
    return temp ;
}

void MainWindow::on_pushButton_clicked()
{
    if(this->ui->lineEdit->text().isEmpty()){
        QMessageBox::about(this,QString::fromLocal8Bit("請先輸入點什麼"),QString::fromLocal8Bit("總要寫點什麼我才能幫你轉換啊！"));
        return;
    }
    QString str = this->ui->lineEdit->text();
    QStringList article = this->Separate(str);
    QString output = "";
    foreach(QString a , article){
        if(this->GetCode(a)!=""){
            output.append(this->GetCode(a));
            output.append(" ");
        }
    }
    if(!output.isEmpty()){
        QClipboard *clipboard =  qApp->clipboard();
        clipboard->setText(output,QClipboard::Clipboard);
        qDebug()<<output;
        this->ui->textBrowser->setText(output);
    }
    else{
        this->ui->textBrowser->setText("請至官網下載 ibus輸入法表格，將輸入法表格檔之boshiamy_t.db檔案放置於本程式同一目錄下。 ");
    }
}

void MainWindow::on_action_triggered()
{
    qApp->exit();
}
